import axios from "axios";
import $store from "@/store/index"
import router from "@/router"
import { ElNotification } from 'element-plus'

//创建axios实例
const service = axios.create({
    baseURL: "/course-api",
    timeout: 5000,
    headers: {
        "Content-type": "application/json;charset=utf-8"
    }
})

//请求拦截
service.interceptors.request.use((config) => {
    config.headers = config.headers || {}
    if (localStorage.getItem("token")) {
        config.headers.token = localStorage.getItem("token") || ""
    }
    return config
})
/**
* http握手错误
* @param result 响应回调,根据不同响应进行不同操作
*/
function errorHandle(result: any) {
    console.log(result.code)
    switch (result.code) {
        case 201:
            ElNotification({
                title: '提示',
                message: result.msg,
                type: 'warning',
            });
            break;
        case 50011:
            setTimeout(() => {
                $store.dispatch('setOutin', {})
                router.push({
                    path: '/login',
                    query: {
                        T: new Date().getTime()
                    }
                });
            }, 1500)
            ElNotification({
                title: '提示',
                message: result.msg,
                type: 'warning',
            });
            break;
        case 50010:
            setTimeout(() => {
                $store.dispatch('setOutin', {})
                router.push({
                    path: '/login',
                    query: {
                        T: new Date().getTime()
                    }
                });
            }, 1500)
            ElNotification({
                title: '提示',
                message: result.msg,
                type: 'warning',
            });
            break;
        case 50012:
            setTimeout(() => {
                $store.dispatch('setOutin', {})
                router.push({
                    path: '/login',
                    query: {
                        T: new Date().getTime()
                    }
                });
            }, 1500)
            ElNotification({
                title: '提示',
                message: result.msg,
                type: 'warning',
            });
            break;
        case 403:
            break;
        default:
            ElNotification({
                title: '提示',
                message: '异常错误！',
                type: 'warning',
            });
    }
}
//响应拦截
service.interceptors.response.use((res) => {
    const code: number = res.data.code
    if (code == 200) {
        return Promise.resolve(res.data);
    }else{
        errorHandle(res.data);
        return Promise.reject(res)
    }
}, (err) => {
    console.log('错误回调', err)
    // 判断请求异常信息中是否含有超时timeout字符串
    if (err.message.includes('timeout')) {
        ElNotification({
            title: '提示',
            message: '网络超时！',
            type: 'error',
        });
    }
    if (err.message.includes('Network Error')) {
        ElNotification({
            title: '提示',
            message: '服务端未启动，或网络连接错误！',
            type: 'error',
        });
    }
    if (err.message.includes('Request failed with status code 401')) {
        ElNotification({
            title: '提示',
            message: '没有访问权限，需要进行身份认证！',
            type: 'error',
        });
    }
    return Promise.reject(err)
})

export default service